// prediction by LDA model
// output
// 1. held-out loglikelihood
// 2. topic membership
// 3. topic proportion
#include <boost/ptr_container/ptr_vector.hpp>
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

#include "ted.hh"
#include "expression.hh"
#include "latent.hh"
#include "lda.hh"

#include <numeric>
#include <algorithm>

typedef geneset_t<expr_pair_t> ep_geneset_t;
typedef boost::ptr_vector<ep_geneset_t> ep_geneset_vec_t;
typedef boost::ptr_vector<expr_pair_set_t> pair_set_vec_t;

struct prog_args_t
{

  explicit prog_args_t() :
      g_file(""), x_file(""), y_file(""), // required args
      mean_file(""), prec_file(""),         //
      output("output"), sample(100)       // options
  {
  }

  std::string g_file;
  std::string x_file;
  std::string y_file;
  std::string mean_file;
  std::string prec_file;
  std::string output;
  size_t sample;
};

void print_help(const char* prog, const prog_args_t& args)
{
  std::cerr << prog << " -g G.txt -x X.txt -y Y.txt -o out" << std::endl;
  std::cerr << "         -mean beta.mean -prec beta.prec" << std::endl;
  std::cerr << "         -s sample" << std::endl;
  std::cerr << std::endl;
  std::cerr
      << " G.txt  geneset / group file (each line contains row-indexes of X,Y)"
      << std::endl;
  std::cerr << " X.txt      control expression file (n x p tab-sep real mat)"
      << std::endl;
  std::cerr << " Y.txt      case expression file (n x p tab-sep real mat)"
      << std::endl;
  std::cerr << " beta.mean  mean coefficient file (result of fitting)"
      << std::endl;
  std::cerr << " beta.prec  precision of coefficient file (result of fitting)"
      << std::endl;
  std::cerr << " out        output files' header (default: " << args.output
      << ")" << std::endl;
  std::cerr << " sample     number of samples (default: " << args.sample << ")"
      << std::endl;
  std::cerr << std::endl;
}

template<typename T>
T get_param(const char* arg, std::string emsg)
{
  T val;
  try
  {
    val = boost::lexical_cast<T>(arg);
  } catch (boost::bad_lexical_cast&)
  {
    err_msg(emsg);
    exit(1);
  }
  return val;
}

bool parse_args(const int argc, const char* argv[], prog_args_t& args_out)
{
  for (int j = 1; j < argc; ++j)
  {
    std::string curr = argv[j];

    if (curr == "-g" && ++j < argc)
    {
      args_out.g_file = argv[j];
    }
    else if (curr == "-x" && ++j < argc)
    {
      args_out.x_file = argv[j];
    }
    else if (curr == "-y" && ++j < argc)
    {
      args_out.y_file = argv[j];
    }
    else if (curr == "-o" && ++j < argc)
    {
      args_out.output = argv[j];
    }
    else if (curr == "-mean" && ++j < argc)
    {
      args_out.mean_file = argv[j];
    }
    else if (curr == "-prec" && ++j < argc)
    {
      args_out.prec_file = argv[j];
    }
    else if (curr == "-s" && ++j < argc)
    {
      args_out.sample = get_param<size_t>(argv[j], "parsing #samples");
    }

  }

  if (args_out.g_file.size() == 0 || args_out.x_file.size() == 0
      || args_out.y_file.size() == 0 || args_out.mean_file.size() == 0
      || args_out.prec_file.size() == 0)
    return false;

  return true;
}

bool read_beta(const char* mean_file, const char* prec_file,
    boost::ptr_vector<func_expr_div_t>& topics)
{
  size_t K = num_rows(mean_file);
  size_t p = num_columns(mean_file);

  if (K < 1 || K != num_rows(prec_file))
    return false;
  if (p != num_columns(prec_file) || p < 2)
    return false;

  const double bmin = -5.;
  const double bmax = 5.;

  // initialize topics
  topics.clear();

  func_expr_div_t::vec_t beta(p - 1);
  func_expr_div_t::vec_t prec(p - 1);

  std::string mean_line;
  std::string sd_line;
  std::ifstream mean_ifs(mean_file, std::ios::in);
  std::ifstream prec_ifs(prec_file, std::ios::in);

  while (std::getline(mean_ifs, mean_line, '\n')
      && std::getline(prec_ifs, sd_line, '\n'))
  {
    std::stringstream mean_ss(mean_line, std::ios::in);
    std::stringstream prec_ss(sd_line, std::ios::in);

    int mean_k = -1, prec_k = -1;
    double b, r;
    mean_ss >> mean_k;
    prec_ss >> prec_k;

    size_t j;
    for (j = 0; j < (p - 1) && (mean_ss >> b && prec_ss >> r); ++j)
    {
      beta[j] = b;
      prec[j] = r;
    }

    // make sure to read p-1 numbers
    if (j != (p - 1) || mean_k != prec_k)
    {
      mean_ifs.close();
      prec_ifs.close();
      return false;
    }

    topics.push_back(new func_expr_div_t(p - 1, bmin, bmax, beta, prec));
  }

  mean_ifs.close();
  prec_ifs.close();
  return true;
}

int main(const int argc, const char* argv[])
{
  prog_args_t args;
  if (!parse_args(argc, argv, args))
  {
    print_help(argv[0], args);
    return 1;
  }

  // read expression data
  expr_pair_vec_t pair_data;
  size_t n, p;
  std::string& x = args.x_file;
  std::string& y = args.y_file;
  assert_msg(read_expr_pair_vec(x.c_str(), y.c_str(), pair_data, n, p),
      "cannot read expression pairs");

  // read beta mean and sd files
  // and determine K
  boost::ptr_vector<func_expr_div_t> topics;
  assert_msg(read_beta(args.mean_file.c_str(), args.prec_file.c_str(), topics),
      "cannot read beta mean and prec");
  size_t K = topics.size();

  // read geneset data
  std::string& g = args.g_file;
  ep_geneset_vec_t genesets;
  double a0 = 1. / ((double) K);
  assert_msg(read_genesets<expr_pair_t>(g.c_str(), pair_data, a0, K, genesets),
      "cannot read genesets");

  gibbs_latent_update_t<expr_pair_t, func_expr_div_t> gibbs_update(topics, args.sample);
  std::for_each( genesets.begin(), genesets.end(), gibbs_update );

  output_argmax(args.output, pair_data, topics);
  output_genesets(args.output, genesets);
  std::ofstream ofs((args.output + ".llik").c_str(), std::ios::out);
  ofs << likelihood(genesets) << std::endl;
  ofs.close();
  return 0;
}
